package com.zhuang.designpattern.singleton;

/**
 * description: 懒汉模式(线程安全)
 * date: 2022/9/14 14:38
 * author: Zhuang
 * version: 1.0
 */
public class Singleton_02 {
    public static void main(String[] args) {
        Singleton02 instance1 = Singleton02.getInstance();
        Singleton02 instance2 = Singleton02.getInstance();

        System.out.println("instance1 = " + instance1);
        System.out.println("instance2 = " + instance2);
    }

}

class Singleton02 {
    private static Singleton02 instance;

    private Singleton02() {

    }

    public static synchronized Singleton02 getInstance() {
        if (null != instance) return instance;
        instance = new Singleton02();
        return instance;
    }
}
